<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Index extends Base
{
    /**
    *小程序部分
    */
    private function httpRequest($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
  	public function openid(){
        if(!empty(input("param.code"))){
            $code=input("param.code");
        }else{
            return json(['code' => 501, 'msg' => '请给code']);exit();
        }
        $url="https://api.weixin.qq.com/sns/jscode2session?appid=wx897a77a94be25594&secret=da595027c56ce62b942e1332ded3e6aa&js_code=".$code."&grant_type=authorization_code";
        $res=$this->httpRequest($url);
        if($res){
            return json(['code' => 200, 'msg' => '成功！','data'=>json_decode($res)]);exit();
        }else{
            return json(['code' => 500, 'msg' => '失败！']);exit();
        }
    }
}